package com.taru.houseapp.config;

import com.taru.houseapp.filter.CorsFilter;
import com.taru.houseapp.filter.TokenIsValidFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class FilterConfig {

    @Resource
    CorsFilter corsFilter;
    @Resource
    TokenIsValidFilter tokenIsValidFilter;

    @Bean
    public FilterRegistrationBean registerCorsFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(corsFilter);
        registration.addUrlPatterns("/*");
        registration.setName("corsFilter");
        registration.setOrder(1);  //值越小，Filter越靠前。
        return registration;
    }

    @Bean
    public FilterRegistrationBean registerTokenIsValidFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        Map<String,String> hashMap=new HashMap<>();
        String str="/login,/register,/house/collection/judge,/user/password/replace,/image/upload";
        hashMap.put("exclude",str);
        registration.setFilter(tokenIsValidFilter);
        registration.addUrlPatterns("/*");
        registration.setName("tokenIsValidFilter");
        registration.setInitParameters(hashMap);
        registration.setOrder(2);
        return registration;
    }

    //如果有多个Filter，再写一个public FilterRegistrationBean registerOtherFilter(){...}即可。
}
